package com.innjia.shiro.utils;

import com.innjia.base.utils.JsonUtil;
import com.innjia.base.utils.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * JSON响应工具类
 * <br>
 * 2017-11-16
 *
 * @author zhouxianglh@gmail.com
 */
public class HttpUtils {
    private static final Logger LOGGER = LoggerFactory.getLogger(HttpUtils.class);

    private HttpUtils() {
    }

    /**
     * 响应JSON
     *
     * @param response
     * @param result
     */
    public static void responseJSON(HttpServletResponse response, Result result) {
        response.setContentType("application/json;charset=UTF-8");
        try (PrintWriter writer = response.getWriter()) {
            writer.write(JsonUtil.getJsonByObj(result));
            writer.flush();
        } catch (IOException e) {
            LOGGER.error("", e);
        }
    }
}
